package com.wantup.demo03xd.part06.atomic;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class AtomicLongFieldUpdaterDemo {

    public static void main(String[] args) {
        AtomicLongFieldUpdater<Student> updater = AtomicLongFieldUpdater.newUpdater(Student.class, "id");

        Student student = new Student(1L, "zhangsan");
        System.out.println(updater.compareAndSet(student, 1L, 10L));
        System.out.println(student.getId());

        AtomicReferenceFieldUpdater<Student, String> newUpdater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
        System.out.println(newUpdater.compareAndSet(student, "zhangsan", "zhaoluowei"));
        System.out.println(student);


    }
}

/**
 *
 */
class Student {

    volatile long id;
    volatile String name;

    public Student() {
    }

    public Student(Long id, String name) {
        this.id = id;
        this.name = name;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}



